package org.kymjs.contacts;


import ohos.agp.components.*;
import ohos.app.Context;
import org.kymjs.contacts.bean.Contact;

import java.util.ArrayList;
import java.util.List;

import static ohos.agp.components.Component.HIDE;
import static ohos.agp.components.Component.VISIBLE;

/**
 * @Author: Bro0cL
 * @Date: 2018/2/5 12:06
 */
public class ContactsAdapter extends BaseItemProvider {
    private Context mContext;
    private List<Contact> mData;
    private int count;

    public ContactsAdapter(Context slice, List<Contact> list, int count) {
        //底部
        list.add(new Contact());
        this.mData = list;
        this.mContext = slice;
        this.count = count;
    }


    // 用于保存列表项中的子组件信息
    public static class DefaultViewHolder {
        Text headerText;
        Text tvLetter;
        Text tvLine;
        Text count;
        DirectionalLayout dl;

        DefaultViewHolder(Component itemView) {
            headerText = (Text) itemView.findComponentById(ResourceTable.Id_contact_title);
            tvLetter = (Text) itemView.findComponentById(ResourceTable.Id_contact_catalog);
            tvLine = (Text) itemView.findComponentById(ResourceTable.Id_contact_line);
            count = (Text) itemView.findComponentById(ResourceTable.Id_count);
            dl = (DirectionalLayout) itemView.findComponentById(ResourceTable.Id_dl);
        }
    }


    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        if (mData != null && position >= 0 && position < mData.size()) {
            return mData.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        DefaultViewHolder holder = null;
        Contact data = mData.get(position);
        if (component == null) {
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_list_contact, null, false);
            holder = new DefaultViewHolder(component);
            component.setTag(holder);
        } else {
            holder = (DefaultViewHolder) component.getTag();
        }
        holder.count.setText(count + "位联系人");
        //如果是第0个那么一定显示#号
        holder.headerText.setText(data.getName());
        if (mData.size() > 1 && position == 0) {
            holder.tvLetter.setVisibility(VISIBLE);
            holder.tvLetter.setText("#");
            holder.tvLine.setVisibility(VISIBLE);
        } else {
            //如果和上一个item的首字母不同，则认为是新分类的开始
            if (mData.size() > 1) {
                Contact prevData = mData.get(position - 1);
                if (data.getFirstChar() != prevData.getFirstChar()) {
                    holder.tvLetter.setVisibility(VISIBLE);
                    holder.tvLetter.setText("" + data.getFirstChar());
                    holder.tvLine.setVisibility(VISIBLE);
                } else {
                    holder.tvLetter.setVisibility(HIDE);
                    holder.tvLine.setVisibility(HIDE);
                }
            }
        }
        if (position == mData.size() - 1) {
            holder.count.setVisibility(VISIBLE);
            holder.dl.setVisibility(HIDE);
        } else {
            holder.count.setVisibility(HIDE);
            holder.dl.setVisibility(VISIBLE);
        }
        return component;
    }

    /**
     * 根据ListView的当前位置获取分类的首字母的Char ascii值
     *
     * @param position int
     * @return int
     */
    public int getSectionForPosition(int position) {
        Contact item = mData.get(position);
        return item.getFirstChar();
    }

    /**
     * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
     *
     * @param section int
     * @return int
     */
    public int getPositionForSection(int section) {
        for (int i = 0; i < getCount(); i++) {
            char firstChar = mData.get(i).getFirstChar();
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 滚动RecyclerView到索引位置
     *
     * @param index index
     * @param lc lc
     */
    public void scrollToSection(int index, ListContainer lc) {
        if (mData == null || mData.isEmpty()) return;
        lc.scrollTo(index);
    }

    public void refresh(List<Contact> datas) {
        if (datas == null) {
            datas = new ArrayList<Contact>();
        }
        datas.add(new Contact());
        this.mData = datas;
        notifyDataChanged();
    }
}

